var host = "http://zeny.free.idcfengye.com"
var global_login_url = ""

function check_order() {

}
function save_order(id) {
    var token = $.cookie("token");
    if (!token) {
		window.location.href = global_login_url;
	}
    var url = host + "/user/api/v1/order/add?videoId="+id
    $("#pay_img").attr('src', url);
}



//获取视频列表
function get_list() {
    $.ajax({

        type: 'get',
        url: host + "/api/v1/video/page?size=30&page=1",
        dataType: 'json',
        success: function (res) {
            var data = res.data.list;
            for (var i = 0; i < data.length; i++) {
                console.info(data[i])
                var video = data[i];
                var price = video.price / 100;

                var template = "<div class='col-sm-6 col-md-3'><div class='thumbnail'>" +
                    "<img src='" + video.coverImg + "'alt='通用的占位符缩略图'>" +
                    "<div class='caption'><h3>" + video.title + "</h3><p>价格:" + price + "元</p>" +
                    "<p><a href='' onclick='save_order(" + video.id + ")' data-toggle='modal' data-target='#myModal' class='btn btn-primary' role='button'>立刻购买</a></p></div></div></div>"

                $(".row").append(template);

            }


        }

    })
}

//获取微信地址
function get_wechat_login() {
    var current_page = window.location.href;
    $.ajax({
        type: 'get',
        url: host + "/api/v1/wechat/login_url?access_page=" + current_page,
        dataType: 'json',
        success: function (res) {
            $("#login").attr("href", res.data);
            global_login_url = res.data;
        }
    })
}

//获取URL上的参数
function get_params() {
    //获取？后面的字符串
    var url = window.location.search;
    var obj = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            obj[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
        }
        return obj;
    }
}

//设置头像和昵称
function set_user_info() {
    var user_info = get_params();
    if (JSON.stringify(user_info) != null) {
        var name = user_info['name'];
        var head_img = user_info['head_img'];
        var token = user_info['token'];
        $("#login").html(name);
        $("#head_img").attr('src', head_img);
        $.cookie('token', token, {expires: 7, path: '/'})
        $.cookie('head_img', head_img, {expires: 7, path: '/'})
        $.cookie('name', name, {expires: 7, path: '/'})
    } else {
		var name = $.cookie("name");
		var head_img = $.cookie("head_img");
		if (name){
			$("#login").html(name);
		}
		if (head_img) {
			$("#head_img").attr('src', head_img);
		}
	}
}

//页面加载完成时调用
$(function () {
    get_list();
    get_wechat_login();
    set_user_info();
})